package com.gxa.shop.dao.cart.Impl;

import com.gxa.shop.bean.pojo.Cart;
import com.gxa.shop.bean.vo.CartVo;
import com.gxa.shop.commons.jdbc.JDBCUtil;
import com.gxa.shop.dao.cart.CartDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * com.gxa.shop.dao.cart.Impl
 * User: 刁彦侨
 * Date: 2024/12/19 15:28
 * motto:   逆水行舟不进则退
 * Description:
 * Version: V1.0
 */
public class CartDaoImpl implements CartDao {
    @Override
    public List<CartVo> findById(String id) {
        String sql = "SELECT c.user_id, c.product_id, c.quantity, p.product_name, p.unit_price, p.product_image, p.product_color, p.product_size, p.product_material, p.weight FROM cart c JOIN product p ON c.product_id = p.product_id WHERE c.user_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString(), id);
        if(query.size()>0){
            List<CartVo> carts = new ArrayList<>();
            for (Map<String, Object> row : query) {
                CartVo cartVo = new CartVo();
                cartVo.setUserId((Integer) row.get("user_id"));
                cartVo.setProductId((Integer) row.get("product_id"));
                cartVo.setQuantity((Integer) row.get("quantity"));
                cartVo.setProductName((String) row.get("product_name"));
                cartVo.setUnitPrice((Double) row.get("unit_price"));
                cartVo.setProductImage((String) row.get("product_image"));
                cartVo.setProductColor((String) row.get("product_color"));
                cartVo.setProductSize((String) row.get("product_size"));
                cartVo.setProductMaterial((String) row.get("product_material"));
                cartVo.setProductWeight((Double) row.get("weight"));
                carts.add(cartVo);
            }
            System.out.println("CartDaoImpl：购物车查询成功");
            return  carts;
        }
        System.out.println("CartDaoImpl：购物车中没有数据");
        return null;
    }

    @Override
    public int insert(Cart cart) {
        String sql = "INSERT INTO cart(user_id, product_id, quantity) VALUES (?, ?, ?)";
        JDBCUtil jdbcUtil = new JDBCUtil();
        List<Object>  parameters=new ArrayList<>();
        parameters.add(cart.getUserId());
        parameters.add(cart.getProductId());
        parameters.add(cart.getQuantity());
        return jdbcUtil.update(sql, parameters.toArray());
    }

    @Override
    public int deleteById(Integer userId, Integer productId) {
        String sql = "DELETE FROM cart WHERE user_id = ? AND product_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        return jdbcUtil.update(sql, userId, productId);
    }

    @Override
    public int deleteByUserId(Integer userId) {
        String sql = "DELETE FROM cart WHERE user_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        return jdbcUtil.update(sql, userId);
    }

    @Override
    public int update(Cart cart) {
        String sql = "UPDATE cart SET quantity = ? WHERE user_id = ? AND product_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        List<Object>  parameters=new ArrayList<>();
        System.out.println(cart.getQuantity());
        System.out.println(cart.getUserId());
        System.out.println(cart.getProductId());
        parameters.add(cart.getQuantity());
        parameters.add(cart.getUserId());
        parameters.add(cart.getProductId());
        return jdbcUtil.update(sql,parameters.toArray());
    }
}
